package chapter6;
import java.util.Scanner;

/****************************************************************
 * Author：icynlau
 * Description: 显示当前的日期和时间(24小时制)  参考  程序清单2-7  6-12
 * 
 * 这道题目比较麻烦，不过算是锻炼自顶向下思考，锻炼写循环的优秀例子了
 * 
 * System.currentTime() 是从1970-01-01 00:00:00 星期四（新中国成立21年）到现在的毫秒数，我们要由此得到日期和时间
 * （考虑当月第一天是星期几，遍历过来考虑到每一年是平年还是闰年，都是很麻烦的）
 * 
 * moth : 0-1月 1-2月
 * 星期：0-Sun 1-Mon
- * Date:2021/4/4
 * ***************************************************************
 */
public class exc6_24 {
	
	
	public static void main(String[] args) {
		
		final int GMT=8;		//北京时间 东八区
		
		long currentSecond=System.currentTimeMillis()/1000%60;
		
		long currentMinute=System.currentTimeMillis()/1000/60%60;
		
		long currentHour=System.currentTimeMillis()/1000/60/60%24;
		
		System.out.print("\t"+getMonthName(getMonth())+" "+getdate()+" "+getYear()+" "
							+getDayName(   getDay(   getYear(), getMonth()   )     )+" "
							+(currentHour+GMT)+":"+currentMinute+":"+currentSecond+"\n");
		
	}
	
	public static boolean isLeapYear(int year) {
		
		return (year%4==0&&year%100!=0 ||year%400==0)? true:false;
		
	}
	
	public static int getYear() {
		int days=365;			//某年的天数
		long numberOfDays=System.currentTimeMillis()/1000/60/60/24+1;//得到至今天数
		int year=1970;//返回结果：当前年份
		for(;numberOfDays>0;year++) {
			if(isLeapYear(year)) {
				numberOfDays-=366;
			}else {
				numberOfDays-=365;
			}
			
		}
		return year-1;
	}
	
	public static long getdate() {//是本月几号
		int year=getYear();		//获取当前年份
		int month=getMonth();
		long numberOfDays=System.currentTimeMillis()/1000/60/60/24+1;//得到至今天数
		for(int i=1970;i<year;i++) {
			if(isLeapYear(i)) {
				numberOfDays-=366;
			}else {
				numberOfDays-=365;
			}
			
		}
		for(int i=0;i<month;i++) {
			
			numberOfDays-=getNumberOfDaysInMonth(year,i);
			
		}
		
		return numberOfDays;
		
		
	}
	
	public static int getMonth() {
		int year=getYear();		//获取当前年份

		long numberOfDays=System.currentTimeMillis()/1000/60/60/24+1;//得到至今天数
		for(int i=1970;i<year;i++) {
			if(isLeapYear(i)) {
				numberOfDays-=366;
			}else {
				numberOfDays-=365;
			}
			
		}
		//求出现在是几月份
		int month=0;
		for(int i=0;numberOfDays>0;i++) {
			
			numberOfDays-=getNumberOfDaysInMonth(year,i);
			month=i;
			
		}
		
		return month;
		
		
	}
	
	public static long getDay(int year,int month) {//获取今天是周几

		long ret=3;  //1970.1.1是礼拜四
		
		long numberOfDays=System.currentTimeMillis()/1000/60/60/24+1;//得到至今天数，别忘了+1才是今天
		
		int someYear=1970,someMonth=0;		//循环最开始的
		for(int i=1970;numberOfDays>0;i++) {
			
			for(someMonth=0;someMonth<12;someMonth++) {
				numberOfDays-=getNumberOfDaysInMonth(someYear,someMonth);
				if(numberOfDays<0) {
					//剩余天数不足以到达下个月
//					System.out.print("就是这个月了："+someYear+" 年 "+(someMonth+1)+"月"+"\n");
					numberOfDays+=getNumberOfDaysInMonth(someYear,someMonth);
//					System.out.print("本月过了"+numberOfDays+"天\n");
					ret=(ret+numberOfDays)%7;
					return ret;
				}
				ret=(ret+getNumberOfDaysInMonth(someYear,someMonth))%7; 		//经过了someYear 的someMonth ,下个月月初是星期几
//				System.out.print(someYear+" 年 "+(someMonth+1)+"月"+"\n");
			
			}
			
			someYear++;
			
		}
		
		return ret;
	}
	
	
	
	public static int getNumberOfDaysInMonth(int year,int month) {
		int ret=31;
		
		int[] array= {31,28,31,30,31,30,31,31,30,31,30,31};
		
		if(isLeapYear(year)) {
			
			array[1]=29;
					
		}
		
		ret=array[month];
		
		return ret;
	}
	
	public static String getDayName(long dayL) {
		
		String dayName="";
//		int day=int(dayL);		//不让转换
//		switch(day) {
//		case 0:dayName="Sunday";break;
//		case 1:dayName="Monday";break;
//		case 2:dayName="Tuesday";break;
//		case 3:dayName="Wednesday";break;
//		case 4:dayName="Thursday";break;
//		case 5:dayName="Friday";break;
//		case 6:dayName="Saturday";break;
//		}
		
		if(dayL==0) {
			dayName="Sunday";
		}else if(dayL==1) {
			dayName="Monday";
		}else if(dayL==2) {
			dayName="Tuesday";
		}else if(dayL==3) {
			dayName="Wednesday";
		}else if(dayL==4) {
			dayName="Thursday";
		}else if(dayL==5) {
			dayName="Friday";
		}else if(dayL==6) {
			dayName="Saturday";
		}
		
		return dayName;
		
	}
	
	public static String getMonthName(int month) {
		
		String monthName="";
		
		switch(month) {
		case 0:monthName="January";break;
		case 1:monthName="February";break;
		case 2:monthName="March";break;
		case 3:monthName="April ";break;
		case 4:monthName="May";break;
		case 5:monthName="June";break;
		case 6:monthName="July";break;
		case 7:monthName="August";break;
		case 8:monthName="September";break;
		case 9:monthName="October";break;
		case 10:monthName="November";break;
		case 11:monthName="December";break;
		}
		
		return monthName;
		
	}
	
	

}
